package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.ImportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("ordersetting")
public class OrderSettingController {

    @DubboReference
    private OrderSettingService orderSettingService;

    /**
     * @Author: 15041
     * @Date: 20:49 2021/1/9
     * @Parms: [year, month]
     * @ReturnType: com.itheima.health.entity.Result
     * Description: 查询所有预约数据
     */
    @GetMapping("findByMonth")
    public Result findByMonth(Integer year, Integer month) {
        log.info("预约设置-查询所有预约传参controller层传参：year：{} ，month ：{}", year, month);
        List<OrderSetting> orderSettingList = orderSettingService.findByMonth(year, month);
        log.info("预约设置-查询所有预约结果：{}", orderSettingList);
        List<Map<String, Object>> list = new ArrayList<>();
        parseVal(orderSettingList, list);
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS, list);
    }

    /**
     * @Author: 15041
     * @Date: 20:49 2021/1/9
     * @Parms: [orderSettingList, list]
     * @ReturnType: void
     * Description: 将ordersettingList转变成所需数据
     */
    private void parseVal(List<OrderSetting> orderSettingList, List<Map<String, Object>> list) {
        //遍历ordersettingList集合
        for (OrderSetting orderSetting : orderSettingList) {
            //创建map存放 data天 number 可预约的人数 reservations 已预约人数
            Map<String, Object> map = new HashMap<>();
            //将数据存到map中 如果数据库没有赋值，因为是Integer类型的默认值是null 所以用三元表达式判断一下是否有值，没有就选0
            map.put("number", orderSetting.getNumber() == null ? 0 : orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations() == null ? 0 : orderSetting.getReservations());
            //日期转换获取
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(orderSetting.getOrderDate());
            int date = calendar.get(Calendar.DAY_OF_MONTH);
            map.put("date", date);
            //将封装好的map添加到集合中
            list.add(map);
        }
    }

    /**
     * @Author: 15041
     * @Date: 21:14 2021/1/10
     * @Parms: [orderSetting]
     * @ReturnType: com.itheima.health.entity.Result
     * Description: 设置预约人数
     */
    @PostMapping("editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        log.info("预约设置-设置预约人数传参controller层传参：{}", orderSetting);
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true, MessageConst.ORDERSETTING_SUCCESS);

    }

    @PostMapping("upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file) {
        //获取文件名称
        String filename = file.getOriginalFilename();
        //判断如果为空返回失败
        if (StringUtils.isEmpty(filename)) {
            return new Result(false, "文件名不能为空");
        }
        List<List<Object>> lists;

        try {
            //判断后缀根据后缀调用方法 xls是2003版的
            if (filename.endsWith(".xls")) {
                lists = ImportUtil.read2003Excel(file.getInputStream());
            } else {
                lists = ImportUtil.read2007Excel(file.getInputStream());
            }
            //将List<List<Object>>转换为List<OrderSetting>
            List<OrderSetting> list = parseToOrderSettingList(lists);
            //调用service完成保存
            orderSettingService.add(list);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(true, MessageConst.UPLOAD_SUCCESS);
    }

    /**
     * @Author: 15041
     * @Date: 21:54 2021/1/10
     * @Parms: [lists]
     * @ReturnType: java.util.List<com.itheima.health.pojo.OrderSetting>
     * Description: 将List<List<Object>>转成List<OrderSetting>的方法
     */
    private List<OrderSetting> parseToOrderSettingList(List<List<Object>> lists) throws ParseException {
        //创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<OrderSetting> list = new ArrayList<>();
        //从第二行开始获取数据 因为第一行是数据类型的名字
        for (int i = 1; i < lists.size(); i++) {
            //得到每一行数据
            List<Object> rowList = lists.get(i);
            OrderSetting orderSetting = new OrderSetting();
            //设置日期 根据模板得知第一个元素是日期，所以获取索引0上的元素
            orderSetting.setOrderDate(sdf.parse((String) rowList.get(0)));
            //设置预约人数
            orderSetting.setNumber(Integer.valueOf(String.valueOf(rowList.get(1))));
            list.add(orderSetting);
        }
        return list;
    }

}
